/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Dao;

import com.lcfgly.Database.ShopPrices;
import com.lcfgly.DatabaseFactory.DataFactoryInterface;
import com.lcfgly.Interface.GetCacheMark;
import com.lcfgly.Interface.UpdateCacheMark;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * Prices表操作
 * @author lcfgly
 */
@Repository
public class PriceImpl implements Serializable{

    private final String table="shop_prices";
    private final String sqlheader="SELECT id,shopid,product,producttype,price,updatetime,priceimg FROM shop_prices ";

    @Autowired
        @Qualifier("databaseshoprice")
    private DataFactoryInterface dataFactory;
    
    /**
     * 更新价格
     * @param arg0
     * @param shopid 用户ID
     * @return
     * @throws SQLException 
     */
    @UpdateCacheMark("shopPrices_${1}_${0.shopid}")
    public int update(ShopPrices arg0,int shopid) throws SQLException {
        return dataFactory.update(table,arg0, "id=? AND shopid =?", arg0.getId(),shopid);
    }
   
    
    /**
     * 按店铺商品查找
     * @param shopid
     * @param product
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopPrices_${0}_${1}")
    public ShopPrices findbyShopidAndProduct(int shopid, int product) throws SQLException
    {
        String sql=sqlheader+" WHERE shopid=? AND product=?";
        return dataFactory.one(ShopPrices.class,sql, shopid,product);
    }
    
    /**
     * 按店铺商品查找
     * @param shopid
     * @param page
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("shopfindbyShopid_${0}_${1}")
    public Map findbyShopid(int shopid, int page) throws SQLException
    {
        String sql=sqlheader+" WHERE shopid=? order by updatetime desc";
        String sql2="SELECT count(*) FROM "+table+" WHERE shopid=?";
        int rownums=dataFactory.Getrowsnum(sql2, shopid);
        return dataFactory.Page(ShopPrices.class, sql, rownums, page, 20, shopid);
    }
    
    /**
     * 按照ID删除
     * @param id
     * @param shopid 店铺ID
     * @return
     * @throws SQLException 
     */
    public int deletebyId(Integer id,int shopid) throws SQLException{
        return dataFactory.delete(table,"id=? AND shopid=?", id,shopid);
    }
    
    /**
     * 批量删除
     * @param result
     * @param shopid
     * @return
     * @throws SQLException 
     */
    public int delete(ShopPrices result,int shopid) throws SQLException {
        if(result.getId() == null)
        {
            if(result.getProduct() ==0)
                throw new SQLException("数据不完整！");
            else
                return dataFactory.delete(table,"shopid = ? AND product = ?", shopid,result.getProduct());
        }else
            return deletebyId(result.getId(),shopid);
    }
    
    /**
     * 插入数据
     * @param arg
     * @return
     * @throws SQLException 
     */
    public int insert(ShopPrices arg)throws SQLException{
        return dataFactory.insert(arg, table);
    }
  
    /**
     * 获取产品信息
     * @param shopid
     * @param typeid
     * @param page
     * @return
     * @throws SQLException 
     */
    public Map findProductByShopidAndTypeid(int shopid,int typeid,int page)throws SQLException{
        String sql="SELECT count(*) FROM "+table+" WHERE shopid=? AND producttype =?";
        String sql2=sqlheader+" WHERE shopid=? AND producttype =? Order By updatetime desc";
        int rowsnum=dataFactory.Getrowsnum(sql, shopid,typeid);
        return dataFactory.Page(ShopPrices.class, sql2, rowsnum, page, 20, shopid,typeid);
    }
    
    /**
     * 获取首页的报价列表
     * @param shopid
     * @return
     * @throws SQLException 
     */
    public List indexProductList(int shopid)throws SQLException{
        String sql="SELECT product,producttype,price,updatetime FROM "+table+" WHERE shopid=? Limit 10";
        return dataFactory.rowsWithOutObject(sql, shopid);
    }
    
    /**
     * 获取已报价的产品列表
     * @param shopid
     * @param typeid
     * @return
     * @throws SQLException 
     */
    public List downProductList(int shopid,int typeid)throws SQLException{
        String sql="SELECT product,producttype,price FROM "+table+" WHERE shopid=? AND producttype= ?";
        return dataFactory.rowsWithOutObject(sql, shopid,typeid);
    }
    
    /**
     * 批量获取产品价格
     * @param shopid
     * @param str
     * @return
     * @throws SQLException 
     */
    public List GetPriceForShopid(int shopid,String str)throws SQLException{
        String sql="SELECT product,price FROM "+table+" WHERE shopid =? AND product IN ("+str+")";
        return dataFactory.rowsWithOutObject(sql, shopid);
    }
    
}
